/**
 * All rights Reserved, Copyright (C) WANGJIAN LIMITED 2013
 * FileName: PropertyUtil.java
 * Version:  $Revision$
 * Modify record:
 * NO. |		Date		|		Name		|		Content
 * 1   |	2013-12-26		|	JFTT)FuXiaofeng	|	original version
 */
package com.pl.util;

import java.util.Properties;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

/**
 * class name:PropertyUtil <BR>
 * class description: 读取配置文件工具类 <BR>
 * Remark: <BR>
 * @version 1.00 2013-12-26
 * @author JFTT)FuXiaofeng
 */
public class PropertyUtil {
	
	private static Logger log = LogManager.getLogger();
	
	 private static ReloadableResourceBundleMessageSource messageSource;
	
	 public static void init(){  
		 messageSource = SpringContextUtil.getBean("messageSource");
	 }
	
	/**
	 * Method name: getConfigureProperties <BR>
	 * Description: 读取src下第三方配置文件thirdConfig.properties配置文件内容 <BR>
	 * Remark: <BR>
	 * @param propertyName
	 * @return String<BR>
	 */
	public static String getConfigureProperties(String propertyName) {
		
		if(messageSource == null) {
			init();
		}
		
		//默认先使用系统变量，再使用环境变量，再使用配置文件
		String pro = System.getProperty(propertyName);
		if(pro != null) {
			return pro;
		}
		
		String evn = System.getenv(propertyName);
		if(evn != null) {
			return evn;
		}
		
		//String m = messageSource.getMessage("SYSTEM", null, null);
		//System.out.println(m);
		
		String value = messageSource.getMessage(propertyName, null, null);
		
		//君正培训
		//value = messageSource.getMessage(propertyName, null, new Locale("jzTrain"));
		
		return value;
	}
	
	/**
	 * 打印所有属性
	 */
	public static void printAll() {
		
		try {
			
			Properties prop = new Properties();
	        prop.load(PropertyUtil.class.getClassLoader().getResourceAsStream("configure.properties")); 
	        
	        if(!prop.isEmpty()) {
	        	Set<Object> keySet = prop.keySet();
	        	
	        	for (Object object : keySet) {
					String key = (String) object;
					log.debug("{}={}", key, getConfigureProperties(key));
				}
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
